﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;

namespace jnlamp.Web.Admin
{
    public static class myinfo
    {
        public static string getMenu(HttpContext context)
        {
            string umenu = "";
            DataTable dt = context.Session["user"] as DataTable;
            if (dt != null)
            {
                umenu = dt.Rows[0]["umenu"].ToString();
            }

            return umenu;
        }
        public static string getUsername(HttpContext context)
        {
            string uname = "";
            DataTable dt = context.Session["user"] as DataTable;
            if (dt != null)
            {
                uname = dt.Rows[0]["uname"].ToString();
            }

            return uname;
        }
        public static int getID(HttpContext context)
        {
            int id = 0;
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["uid"].ToString());
                }
            }
            return id;
        }
        public static string getPname(HttpContext context)
        {
            string id = "";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = dt.Rows[0]["pname"].ToString();
                }
            }
            return id;
        }
        public static bool isLogin(HttpContext context)
        {            
            if (context.Session["user"] != null)
            {
                return true;
            }
            else
            {
                return false;
            }            
        }
        public static int getLevel(HttpContext context)
        {
            int id = 0;
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["ulevel"].ToString());
                }
            }
            return id;
        }
        public static string getPCC(HttpContext context)
        {
            string pcc = ",,";
            if (context.Session["user"] != null)
            {
                DataTable dt = context.Session["user"] as DataTable;
                if (dt != null)
                {
                    pcc = dt.Rows[0]["uprovince"].ToString() + "," + dt.Rows[0]["ucity"].ToString() + "," + dt.Rows[0]["ucounty"].ToString();
                }
            }
            return pcc;
        }
        public static string RemoveHtml(string str, int size)
        {
            Regex re = new Regex(@"<[^>]+>|</[^>]+>",
                RegexOptions.IgnoreCase |
                RegexOptions.Multiline |
                RegexOptions.IgnorePatternWhitespace |
                RegexOptions.Singleline);

            string s = re.Replace(str, "").Trim();
            return getStr(s, size);

        }
        public static string RemoveHtml(string str)
        {
            Regex re = new Regex(@"<[^>]+>|</[^>]+>",
                RegexOptions.IgnoreCase |
                RegexOptions.Multiline |
                RegexOptions.IgnorePatternWhitespace |
                RegexOptions.Singleline);

            string s = re.Replace(str, "").Trim();
            return s;

        }
        public static string getStr(string s, int l)
        {
            string temp = s;
            if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
            {
                return temp;
            }
            for (int i = temp.Length; i >= 0; i--)
            {
                temp = temp.Substring(0, i);
                if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
                {
                    return temp;
                }
            }
            return "";
        }
    }
}